const utils={}
utils.install=function(Vue){
  let SIGN_REGEXP = /([yMdhsm])(\1*)/g;
  let DEFAULT_PATTERN = 'yyyy-MM-dd';
  function padding(s, len) {
      var len = len - (s + '').length;
      for (var i = 0; i < len; i++) { s = '0' + s; }
      return s;
  };
  let baseJs={
    setLoca(key,data){
      localStorage.setItem(key,JSON.stringify(data))
    },
    getLoca(key){
      return JSON.parse(localStorage.getItem(key))
    },
    getValName(val,arr,key,name,symbol){
      var str='',symbol=symbol||',';

      if(typeof(val)=='object'){
        for(var i=0;i<val.length;i++){
          for(var j=0;j<arr.length;j++){
            if(val[i]==arr[j][key]){
              str+=i==val.length-1?arr[j][name]:arr[j][name]+symbol;
            }
          }
        }
      }else{
        for(var j=0;j<arr.length;j++){
          if(val==arr[j][key]){
            str+=arr[j][name];
          }
        }
      }

      return str;
    },
    getPickerTxt(val){
      var txtArr=val.split(' '),txt='';
      txtArr.map((item,index)=>{
        if(item!='无'){
          txt+=item+'-';
        }
      })
      return txt.substring(0,txt.length-1)
    },
    jsonCopy:(data)=>JSON.parse(JSON.stringify(data)),
    changePickerData(dataList,dataListKey,arrKey){
      var newList=[],dataListKey=dataListKey||["typename","typecode"],
        arrKey=arrKey||['name','value'];
      for(var i=0;i<dataList.length;i++){
        var obj={};
        if(!dataList[i][dataListKey[0]] || !dataList[i][dataListKey[1]]){
          continue
        }
        obj[arrKey[0]]=dataList[i][dataListKey[0]];
        obj[arrKey[1]]=dataList[i][dataListKey[1]];
        if(arrKey[2]){
          obj[arrKey[2]]=dataList[i][dataListKey[2]];
        }
        newList.push(obj);
      }
      return newList;
    },
    changeArrayToStr(arr,symbol,key){
      var str='',symbol=symbol||','
      if(key){
        for(var i=0;i<arr.length;i++){
          str+=i==arr.length-1?arr[i][key]:arr[i][key]+symbol;
        }
      }else{
        for(var i=0;i<arr.length;i++){
          str+=i==arr.length-1?arr[i]:arr[i]+symbol;
        }
      }

      return str;
    },
    getQueryStringByName: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        var context = "";
        if (r != null)
            context = r[2];
        reg = null;
        r = null;
        return context == null || context == "" || context == "undefined" ? "" : context;
    },
    changeStrToArr:function(str){
     if (str.indexOf(',') > -1) {
       return str.split(',')
     } else {
       return [str]
     }
   },
    changeBaseData:function(dataList,arrKey){
        var newList=[],arrKey=arrKey||["typename","typecode"];
        for(var i=0;i<dataList.length;i++){
          var obj={};
          obj.name=dataList[i][arrKey[0]];
          obj.value=dataList[i][arrKey[0]];
          obj.key=dataList[i][arrKey[1]];
          newList.push(obj);
        }
        return newList;
    },

    formatDate: {


        format: function (date, pattern) {
            pattern = pattern || DEFAULT_PATTERN;
            return pattern.replace(SIGN_REGEXP, function ($0) {
                switch ($0.charAt(0)) {
                    case 'y': return padding(date.getFullYear(), $0.length);
                    case 'M': return padding(date.getMonth() + 1, $0.length);
                    case 'd': return padding(date.getDate(), $0.length);
                    case 'w': return date.getDay() + 1;
                    case 'h': return padding(date.getHours(), $0.length);
                    case 'm': return padding(date.getMinutes(), $0.length);
                    case 's': return padding(date.getSeconds(), $0.length);
                }
            });
        },
        parse: function (dateString, pattern) {
            var matchs1 = pattern.match(SIGN_REGEXP);
            var matchs2 = dateString.match(/(\d)+/g);
            if (matchs1.length == matchs2.length) {
                var _date = new Date(1970, 0, 1);
                for (var i = 0; i < matchs1.length; i++) {
                    var _int = parseInt(matchs2[i]);
                    var sign = matchs1[i];
                    switch (sign.charAt(0)) {
                        case 'y': _date.setFullYear(_int); break;
                        case 'M': _date.setMonth(_int - 1); break;
                        case 'd': _date.setDate(_int); break;
                        case 'h': _date.setHours(_int); break;
                        case 'm': _date.setMinutes(_int); break;
                        case 's': _date.setSeconds(_int); break;
                    }
                }
                return _date;
            }
            return null;
        }

    },
    // 获取当前月的第一天
    getCurrentMonthFirst(){
      var date=new Date();
      date.setDate(1);
      return this.formatDate.format(date,'yyyy-MM-dd');
    },
    // 获取当前月的最后一天
    getCurrentMonthLast(){
      var date=new Date();
      var currentMonth=date.getMonth();
      var nextMonth=++currentMonth;
      var nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
      var oneDay=1000*60*60*24;
      return this.formatDate.format(new Date(nextMonthFirstDay-oneDay),'yyyy-MM-dd');
    },
    //获取当前月
    getCurrentMonth:function(){
        var newDate = new Date();
        var currentMonth = newDate.getMonth()+1;
        var start = newDate.getFullYear()+'-'+currentMonth+'-1';
        start = new Date(start);
        var nextStart = new Date(start.getFullYear(),start.getMonth()+1,start.getDate());
        var end = new Date(nextStart.getTime()-1000*60*60*24);
        return [start,end];
    },
    //获取当前一周日期范围
    getCurrentWeek:function(){
      var now = new Date(); 
    var nowTime = now.getTime(); 
    var day = now.getDay();
    var oneDayLong = 24*60*60*1000 ; 
    var MondayTime = nowTime - (day-1)*oneDayLong; 
    var SundayTime =  nowTime + (7-day)*oneDayLong;
    var monday = this.formatDate.format(new Date(MondayTime),'yyyy-MM-dd');
    var sunday = this.formatDate.format(new Date(SundayTime),'yyyy-MM-dd');
    return [monday,sunday];
    },
    getSrcUrl(url,data){
      let token=Vue.prototype.$native.getCache('token');
      let urls=this.api;
      if(url.indexOf('?')>-1){
        urls+=url+'&token='+token;
      }else{
        urls+=url+'?token='+token;
      }
      if(data){
        for(var i in data){
          urls+='&'+i+'='+data[i]
        }
      }
      return urls;
    },
    api:Vue.prototype.baseApi,
    resetForm(obj,formName) {
      obj.$refs[formName].resetFields();
    }
  }
  Vue.prototype.baseJs= baseJs;
  return baseJs;
}
export default utils
